import { createRouter,createWebHashHistory } from 'vue-router'
import { ElMessage } from 'element-plus'

// import { defineAsyncComponent } from 'vue'
// const _import = (path) => defineAsyncComponent(()=>import(`../pages/${path}.vue`))
const _import = (path) => () => import(`../pages/${path}.vue`)
const _importAll = (path,path2) => () => import(`../pages/${path}/${path2}.vue`)

import { decrypt } from '../core/util'

import  Pinia  from '../store/pinia'
import { userStore } from '../store'
const uStore = userStore(Pinia)

//路由配置
const routes = [
    {
        path:'/',
        redirect:'/home'
    },
    {
        path:'/',
        name:'index',
        component:_import('index'), //进到app.vue的vue-router标签里
        children:[
            {
                path:'/home',
                name:'home',
                meta:{
                    title:'系统首页'
                },
                component:_import('home')
            },
            {
                path:'/user',
                name:'user',
                meta:{
                    title:'用户列表'
                },
                component:_importAll('user','user')
            },
            {
                path:'/user/center',
                name:'userCenter',
                meta:{
                    title:'个人中心'
                },
                component:_importAll('user','center')
            },
            {
                path:'/set',
                name:'set',
                meta:{
                    title:'系统设置',
                    permission: true
                },
                component:_importAll('set','set')
            },
            {
                path:'/goods/list',
                name:'goodList',
                meta:{
                    title:'商品列表'
                },
                component:_importAll('goods','list')
            },
            {
                path:'/goods/add:id?',
                name:'goodAdd',
                meta:{
                    title:'添加商品',
                    permission: true
                },
                component:_importAll('goods','add')
            },
            {
                path:'/article/list',
                name:'articleList',
                meta:{
                    title:'文章列表'
                },
                component:_importAll('article','list'),
            },
            {
                path:'/article/add:id?',
                name:'articleAdd',
                meta:{
                    title:'文章编辑',
                    permission: true
                },
                component:_importAll('article','add'),
            },
            {
                path:'/other/chatroom',
                name:'otherChatroom',
                meta:{
                    title:'聊天室',
                },
                component:_importAll('other','chatroom'),
            }
            
        ]
    },
    {
        path:'/login',
        name:'login',
        meta:{
            title:'登录'
        },
        component:_import('login')
    },
    {
        path:'/:pathMatch(.*)*',
        name:'notFound',
        meta:{
            title:'找不到页面'
        },
        component:_import('404')
    },
    {
        path:'/404',
        name:'404',
        meta:{
            title:'找不到页面'
        },
        // component:_import('home/home')
        component:_import('404')
    },
]

//创建一个路由器实例，传递routes配置
const router = createRouter({
    history:createWebHashHistory(),
    routes:routes
})

//导航守卫
router.beforeEach((to,from,next)=>{
    document.title = to.meta.title || 'Greyson后台系统'
    const token = localStorage.getItem('token') || ''
    const userId = localStorage.getItem('user_id') || ''
    //有token和id
    if(token&&userId){
        //超级管理员才能去
        if(to.meta.permission){
            const isAdmin = decrypt(localStorage.getItem('sec'))
            if(isAdmin == '1'){
                next()
            }else{
                ElMessage.error('没有访问权限！您不是超级管理员！')
                next('/404')
            }
        //全部人都可以去
        }else{
            console.log('next')
            next()
        }
    //无token或id
    }else{
        //无论想去哪，都只能跳到登录页
        if(to.path == '/login'){ 
            localStorage.clear()
            ElMessage('请登录！')
            next()
        }
        else{ 
            localStorage.clear()
            ElMessage('请登录！')
            next('/login')
        }
    }
})

export default router